package com.erp.erp_ui.Role;

import com.erp.erp_entitys.LogDB.LogDBEntity;
import com.erp.erp_entitys.Role.RoleEntity;
import com.erp.erp_entitys.Role.RoleExample;
import com.erp.erp_entitys.RoleGroup.RoleGroupEntity;
import com.erp.erp_entitys.User.UserEntity;
import com.erp.erp_servers.Role.IRoleService;
import com.erp.erp_servers.RoleGroup.IRoleGroupService;
import com.erp.erp_servers.log.ILogDBService;
import com.erp.erp_ui.base.Base;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import com.erp.utils.WebApiUtils;
import com.ys.openapi.organization.department.Role;
import com.ys.openapi.organization.entity.RoleGroupVo;
import com.ys.openapi.organization.entity.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName RoleControl
 * @Description 角色控制类
 * @Author songjizhen
 * @Date 2020-07-04 10:22
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "role")
 public class RoleControl extends Base {
    @Autowired
    IRoleService roleService;
    @Autowired
    IRoleGroupService roleGroupService;
    @Autowired
    ILogDBService logDBService;
    //云枢接口角色方法类
    Role role=new Role();
    /**
     * 根据主键删除角色
     * @param ids
     * @return
     */
    @RequestMapping(value = "delete")
    @ResponseBody
     public R<Integer> delete(String[] ids){
        try {
            UserEntity user = (UserEntity)this.getBean("user");
            List<Integer> list=new ArrayList<Integer>();
            for(int i=0;i<ids.length;i++){
                list.add(Integer.parseInt(ids[i].replace("[","").replace("]","")));
            }
            RoleExample example=new RoleExample();
            RoleExample.Criteria criteria = example.createCriteria();
            criteria.andIdIn(list);
            List<RoleEntity> roleEntities = roleService.selectByExample(example);
            int num = roleService.deleteByExample(example);
            try {
                //同步云枢开始
                List<String> li=new ArrayList<>();
                for(int i=0;i<roleEntities.size();i++){
                    li.add(roleEntities.get(i).getName());
                }
                WebApiUtils wau=new WebApiUtils();
                Object result= role.delete_role(li,wau.getTokenString());
                //int resultCode=((JSONObject)result).getIntValue("errcode");
                logDBService.insert(new LogDBEntity(user.getName()+"("+user.getCode()+")","delete(删除)","角色=>"+li.toString()));
                //结束
            }catch (Exception e){
                num=num +10000;
            }
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 新增角色
     * @param record
     * @return
     */
    @RequestMapping(value = "insert",method = RequestMethod.POST)
    @ResponseBody
     public R<Integer> insert(@RequestBody RoleEntity record){
        try {
            UserEntity user = (UserEntity)this.getBean("user");
            if(this.isExist(record.getCode())){
                record.setCode(System.currentTimeMillis()+"");
            }
            int num = roleService.insertSelective(record);
            try {
                //同步云枢开始
                Role role=new Role();
                RoleVo rolevo=new RoleVo();
                rolevo.setCode(record.getCode());
                rolevo.setRoleName(record.getName());
                rolevo.setRoleType("OTHER");
                rolevo.setRoleGroupId("");
                rolevo.setRoleGroupName("");
                WebApiUtils wau=new WebApiUtils();
                Object result= role.add_role(rolevo,wau.getTokenString());
                //int resultCode=((JSONObject)result).getIntValue("errcode");
                logDBService.insert(new LogDBEntity(user.getName()+"("+user.getCode()+")","insert(新增)","角色=>"+record.getName()));
                //结束
            }catch (Exception e ){
                num =num +10000;
            }
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
private  boolean isExist(String code){
    RoleEntity record=new RoleEntity();
    record.setCode(code);
    List<RoleEntity> roleEntities = roleService.selectByExample(record);
    return  roleEntities.size()>0;
}
    /**
     * 分页查询角色
     * @param
     * @return
     */
    @RequestMapping(value = "selectByPage")
    @ResponseBody
   public R<List<RoleEntity>> selectByPage(String name,String code){//@RequestBody RoleEntity record,String pageNum,String pageSize
       try {
           RoleEntity record=new RoleEntity();
           record.setCode(code);
           record.setName(name);
           List<RoleEntity> roleEntities = roleService.selectByExample(record);
           return R.build(roleEntities);
       }catch (Exception e){
           e.printStackTrace();
           return R.buildError(ResultStatusEnum.ERROR);
       }
   }

    /**
     * 查询所有角色
     * @return
     */
    @RequestMapping(value = "selectAll")
    @ResponseBody
    public R<List<RoleEntity>> selectAll(){
        try {
            RoleEntity record=new RoleEntity();
            List<RoleEntity> roleEntities = roleService.selectByExample(record);
            RoleGroupEntity rgroup=new RoleGroupEntity();
            List<RoleGroupEntity> roleGroupEntities = roleGroupService.selectByExample(rgroup);

            for(int i=0;i<roleGroupEntities.size();i++){
                RoleGroupEntity rg = roleGroupEntities.get(i);
                RoleEntity re=new RoleEntity();
                re.setName(rg.getName());
                re.setCode(rg.getCode());
                re.setId(rg.getId());
                //re.setType();
                roleEntities.add(re);
            }
            return R.build(roleEntities);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 根据code查询角色
     * @param code
     * @return
     */
    @RequestMapping(value = "selectByCode")
    @ResponseBody
    public R<RoleEntity> selectByCode(String code){
        try {
            RoleEntity record=new RoleEntity();
            record.setCode(code);
            List<RoleEntity> roleEntities = roleService.selectByExample(record);
            return R.build(roleEntities.get(0));
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
    /**
     * 根据主键更新角色
     * @param record
     * @return
     */
    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ResponseBody
     public R<Integer> update(@RequestBody RoleEntity record){
        try {
            UserEntity user = (UserEntity)this.getBean("user");
            //根据编码查询角色
            RoleEntity record2=new RoleEntity();
            record2.setCode(record.getCode());
            List<RoleEntity> roleEntities = roleService.selectByExample(record2);
            String oldname=roleEntities.get(0).getName();
             int num = roleService.updateByPrimaryKeySelective(record);
            try {
                //同步云枢开始
                Role role=new Role();
                RoleVo rolevo=new RoleVo();
                rolevo.setCode(record.getCode());
                rolevo.setRoleName(record.getName());
                rolevo.setRoleType("OTHER");
                rolevo.setRoleGroupId("");
                rolevo.setRoleGroupName("");
                rolevo.setRoleId(oldname);
                WebApiUtils wau=new WebApiUtils();
                Object result= role.modify_role(rolevo,wau.getTokenString());
                //int resultCode=((JSONObject)result).getIntValue("errcode");
                logDBService.insert(new LogDBEntity(user.getName()+"("+user.getCode()+")","update(更新)","角色=>"+record.getName()));
                //结束
            }catch (Exception e){
                num = num +10000;
            }
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
    /**
     * 判断名称是否存在
     * @param name
     * @return
     */
    @RequestMapping(value = "queryByName",method = RequestMethod.GET)
    @ResponseBody
    public R<Boolean> queryByName(String name){
        try {
            RoleExample example=new RoleExample();
            RoleExample.Criteria criteria = example.createCriteria();
            criteria.andNameEqualTo(name);
            List<RoleEntity> roleEntities = roleService.selectByExample(example);
            return R.build(roleEntities.size()==0?false:true);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

}
